﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QuanLyThuVien1.DAO;
using QuanLyThuVien1.BUS;
using QuanLyThuVien1.DTO;

namespace QuanLyThuVien1.GUI
{
    public partial class Fr_ThanhLy : Form
    {
        public Fr_ThanhLy()
        {
            InitializeComponent();
        }
        private void Fr_ThanhLy_Load(object sender, EventArgs e)
        {
            try
            {
                DataTable d = new DataTable();
                Sach_Bus s = new Sach_Bus();
                d = s.LayThongTinThanhLySach();
                dg_ThanhLy.DataSource = d;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message); 
            }

        }

        private void btn_Thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_ThanhLy_Click(object sender, EventArgs e)
        {
            try
            {
                CThanhLySach_Bus t = new CThanhLySach_Bus();
                CThanhLySach t1 = new CThanhLySach();
                Sach_Bus s = new Sach_Bus();
                

                int n = dg_ThanhLy.Rows.Count;
                for (int i = 0; i < n - 1; i++)
                {
                    if (dg_ThanhLy.Rows[i].Cells[2].Value.Equals(1))
                    {
                        t1.MaNguoiThanhLy = Frm_ManHinhChinh.taiKhoan;
                        t1.TenNguoiThanhLy = tb_NguoiThanhLy.Text;
                        t1.MaSach = dg_ThanhLy.Rows[i].Cells[3].Value.ToString();
                        t1.LyDoThanhLy = dg_ThanhLy.Rows[i].Cells[0].Value.ToString();
                        t1.NgayThanhLy = DateTime.Parse(dtp_Ngay.Text);
                        t1.SoCuon = int.Parse(dg_ThanhLy.Rows[i].Cells[1].Value.ToString());
                        t.IntsertBangThanhLySach(t1);
                        s.GiamSachSauKhiThanhly_Bus(t1.MaSach, t1.SoCuon);
                        
                    }
                }
                MessageBox.Show("Đã Thanh Lý Sách Thành Công", "Thông Báo");
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Bạn Thiếu Thông Tin chọn lý do Thanh lý", "Thông Báo");
            }
        }

        private void dg_ThanhLy_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
        
            if (e.ColumnIndex == 2)
            {
                if (this.dg_ThanhLy.Rows[e.RowIndex].Cells[2].Value.Equals(0))
                {
                    this.dg_ThanhLy.Rows[e.RowIndex].Cells[2].Value = 1;
                    MessageBox.Show("Chon duoc " + this.dg_ThanhLy.Rows[e.RowIndex].Cells[2].Value.ToString());
                }
                else
                {
                    this.dg_ThanhLy.Rows[e.RowIndex].Cells[2].Value = 0;
                    MessageBox.Show("Bo chon " + this.dg_ThanhLy.Rows[e.RowIndex].Cells[2].Value.ToString());
                }
            }
        }

        
    }
}